<?php
	require("event-config.php");

	if(!isset($_GET['p']))
    {
        header("Location: error.php");
    }
    else
    {
    	$event_id = $_GET['p'];
    	$query = "SELECT `name`, 
    	DATE_FORMAT(`start_date`,'%d-%m-%Y %H:%i') AS `start_date`, 
    	DATE_FORMAT(`end_date`,'%d-%m-%Y %H:%i') AS `end_date`,
    	DATE_FORMAT(`start_date`,'%d-%m-%Y-%H-%i') AS `start_time`, 
    	DATE_FORMAT(`end_date`,'%d-%m-%Y-%H-%i') AS `end_time`,
    	`benefit`, `description`, `slot` 
    	FROM $EVENT WHERE `event_id` = '$event_id'";

    	$result = first_row($query);
    	if($result){
    		$name 			= $result['name'];
			$start_date 	= $result["start_date"];
			$end_date 		= $result["end_date"];
			$start_time 	= $result["start_time"];
			$end_time 		= $result["end_time"];
			$benefit 		= $result["benefit"];
			$description 	= $result["description"];
			$slot 			= $result["slot"];
    	}
	    else{
	    	header("Location: error.php");
	    }
    }
?>

<html>
<head>
	<title><?php echo $name; ?></title>
	<meta charset='utf-8'/>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
	<header>
        <div class="header-wrapper">
            <img src="img/logo.png" class="header-logo" />
            <?php 
            	if(get_user_role() == "administrator")
				{
					echo "<p class='header-title'><a class='header-link' href='index.php'>Quản lí sự kiện</a></p>
            			  <p class='header-title'><a class='header-link' href='create-event.php'>Tạo sự kiện</a></p>";
				}
				else
				{
					echo "<p class='header-title'>$name</p>";
				}
             ?>
        </div>
    </header>
	
    <div class='container'>
        <div class='post-wrapper'>
			<?php 
				echo "<h2>$name</h2>";
				if($start_date != "00-00-0000 00:00")
					echo "<span class='post-detail'>Ngày bắt đầu: $start_date</span>";
				if($end_date != "00-00-0000 00:00")
					echo "<span class='post-detail'>Ngày kết thúc: $end_date</span>";
				if($benefit)
					echo "<p class='post-detail'>Quyền lợi: $benefit</p>";
				if($description)
					echo "<div>$description</div>";

				if(get_user_role() == "non-user")
				{
					echo "<p class='post-detail'>Để tham gia bạn phải đăng nhập: </p>";
					wp_login_form();
				}
				else
				{
					//Check thoi gian dang ki
					$now = date("d-m-Y-H-i");
					$eventStat = "ok";
					if ($start_date != "00-00-0000 00:00" && compare_date($now, $start_time) == 2) {
						$eventStat = "not_start";
					}
					elseif ($end_date != "00-00-0000 00:00" && compare_date($now, $end_time) == 1) {
						$eventStat = "time_up";
					}

					//Check so slot
					$query = "SELECT `slot` FROM `$EVENT` WHERE `event_id` = '$event_id'";
					$result = first_row($query);
					$slot = $result['slot'];
					echo "<p class='post-detail'>Số lượng suất: $slot</p>";

					//Xem user da dang ki hd nay chua
					$user_login = wp_get_current_user()->user_login;
					$query = "SELECT * FROM $EVENT_JOIN WHERE `event_id` = '$event_id' AND `user_login` = '$user_login'";
					if(first_row($query))
					{
						echo "<span class='post-detail'>Bạn đã đăng kí hoạt động này</span>";
						if($eventStat == "time_up"){
							echo "<span class='post-detail'>Thời gian hiệu chỉnh đã hết</span>";
						}
						else{
						echo "<a href='cancel-register.php?p=$event_id'>
								<button class='btn bt-cancel'>Huỷ tham gia</button>
							</a>";
						}
					}
					else
					{
						if($eventStat == "time_up"){
							echo "<span class='post-detail'>Thời gian đăng kí đã hết</span>";
						}
						elseif ($eventStat == "not_start") {
							echo "<span class='post-detail'>Hoạt động chưa bắt đầu</span>";
						}
						else {
							//Get name
						    $first_name = wp_get_current_user()->user_firstname;
						    $last_name = wp_get_current_user()->user_lastname;

							//Get phone number & email
							$query = "SELECT `phone_number`, `user_email`, `user_facebook` FROM `wp_users` WHERE `user_login` = '$user_login'";
							$result = first_row($query);
							$phone_number 	= $result['phone_number'];
							$user_email 	= $result['user_email'];
							$user_facebook  = $result['user_facebook'];

						    //get poll
						    $poll = get_poll($event_id);
						    $poll_question = "";
						    if ($poll != null) {
						    	$poll_question 	= $poll['poll_question'];
						    	$poll_type 		= $poll['poll_type'];
						    	//get answer
						    	$answer = get_answer($event_id);
						    }

							//Get error
							$error = "";
							if (isset($_GET['error'])) {
								switch ($_GET["error"]) {
									case 0:
										$error = "<span class='post-detail error' id='reg-error'>Chưa điền tên</span>";
										break;
									case 1:
										$error = "<span class='post-detail error' id='reg-error'>Số điện thoại không hợp lệ</span>";
										break;
									default:
										$error = "";
										break;
								}
							}

							//Show the form
							echo "
							<form method='POST' class='form_register' action='register.php?p=$event_id'>
								<p class='post-detail2'>Điền thông tin</p>
								$error
								<table>
									<tr>
										<td><label>Họ</label></td>
										<td><input type='text' name='last_name' class='reg_form' value='$last_name' /></td>
									</tr>
									<tr>
										<td><label>Tên</label></td>
										<td><input type='text' name='first_name' class='reg_form' value='$first_name' /></td>
									</tr>
									<tr>
										<td><label>Số điện thoại</label></td>
										<td><input type='text' name='phone_number' class='reg_form' value='$phone_number' /></td>
									</tr>
									<tr>
										<td><label>Email</label></td>
										<td><input type='text' name='user_email' class='reg_form' value='$user_email' /></td>
									</tr>
									<tr>
										<td><label>Link Facebook</label></td>
										<td><input type='text' name='user_facebook' class='reg_form' value='$user_facebook' /></td>
									</tr>
								</table>";

								if ($poll_question != "") {
									echo "<p class='post-detail2'>$poll_question</p>";
									while($row = next_row($answer)){
										$answer_id = $row['answer_id'];
										$poll_answer = $row['poll_answer'];
										echo "<input type='$poll_type' name='answer[]' value='$answer_id'>$poll_answer<br>";
									}
								}
							
							echo "
								<br>
								<input type='submit' class='btn bt-edit register' value='Đăng kí'>
							</form>
							";
						}
					}
				}
			?>
			<a href='list-attendee.php?p=<?php echo $event_id; ?>' target='_blank'><button class='btn bt-list'>Danh sách tham gia</button></a>
		</div>
	</div>
</body>
</html>